package com.example.test;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import com.example.test.databinding.ActivitySettingBinding;

public class SettingActivity extends AppCompatActivity {
    ActivitySettingBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //创建sp
        SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);

        binding.logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        binding.updatePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, UpdatePasswordActivity.class);
                startActivity(intent);
            }
        });

        //读取sp中的是否管理员模式
        boolean isAdmin = getSharedPreferences("data", MODE_PRIVATE).getBoolean("isAdmin", false);
        if (isAdmin) {
            binding.admin.setChecked(true);
        } else {
            binding.admin.setChecked(false);
        }

        binding.admin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.admin.isChecked()) {
                    //如果选中，存入true
                    SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                    editor.putBoolean("isAdmin", true);
                    editor.apply();
                } else {
                    //如果未选中，存入false
                    SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                    editor.putBoolean("isAdmin", false);
                    editor.apply();
                }

            }
        });
    }
}
